package com.qqt.csr.web.service;

import com.alibaba.fastjson2.JSONObject;
import com.qqt.csr.common.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ArchiveEventMqProducerService {
    /**
     * 推送聊天消息归档topic事件
     */
    public static final String ARCHIVE_EVENT_TOPIC_CONVERSATION_END = "archive_event_topic:conversation_end_event";

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 推送会话聊天消息归档
     *
     * @param conversationIdList {@link String} 会话id
     */
    public void sendConversationArchiveEvent(List<String> conversationIdList) {
        if (CollectionUtils.isEmpty(conversationIdList)) {
            return;
        }
        JSONObject payload = new JSONObject();
        payload.put("conversationIds", conversationIdList);
        payload.put("source", "ARTIFICIAL");
        SendResult sendResult = rocketMQTemplate.syncSend(ARCHIVE_EVENT_TOPIC_CONVERSATION_END, MessageBuilder.withPayload(payload).build());
        log.info("sendConversationArchiveEvent sendResult={},conversationId:{}", JsonUtil.toJson(sendResult), conversationIdList);
    }
}
